<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Message extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->admin->isLogin ( site_url ( 'login' ) );
		$this->load->model ( "message_model" );
		$this->load->model ("user_model");
	}
	/*首页*/
	public function index($page=1){
		$whereArr = array("status"=>1);
		$count = $this->message_model->count ( $whereArr );
		$params = array(
				"url_template" => site_url ( 'Message/index/{page}' ),
				"page_count" => $count,
				"page_now" => $page,
				"page_size" => 10
		);
		$params["total_pages"] = ceil ( $params['page_count'] / $params['page_size'] );
		$this->load->library ( 'paging', $params );
		$message_list = $this->message_model->result($whereArr,$page, $params['page_size'],"sendtime@DESC");
		foreach ($message_list as $key => $value){
			$suser = $this->change_uid_to_user($value['suid']);
			$ruser = $this->change_uid_to_user($value['ruid']);
			$message_list[$key]['slogo'] = ltrim($suser['logo'],".");
			$message_list[$key]['susername'] = $suser['username'];
			$message_list[$key]['rlogo'] = ltrim($ruser['logo'],".");
			$message_list[$key]['rusername'] = $ruser['username'];
		}
		$data['message_list'] = $message_list;
		$this->load->view("message_index",$data);
	}
	/*
	 *删除多条短信 
	 */
	public function duodel(){
		$ids = $this->input->post('ids');
		if(empty($ids))
		{
			$flag =array('status'=>-1,'info'=>'请选择数据!!');
		}else{
			$dataArr=array("status"=>0);
			$result=$this->message_model->update_in($dataArr,'mid',$ids);
			if($result){
				$flag = array('status'=>1,'info'=>'删除多条短信成功!');
			}else{
				$flag = array('status'=>0,'info'=>'删除多条短信失败!');
			}
		}
		echo json_encode($flag);
	}
	/*
	 *删除单条短信 
	 */
	public function del($mid = 1){
		$whereArr = array("mid"=>$mid);
		$dataArr = array("status"=>0);
		$result = $this->message_model->update($dataArr,$whereArr);
		if($result){
			$data['status'] = 1;
			$data['info'] = "短信删除成功!";
		}else{
			$data['status'] = 0;
			$data['info'] = "短信删除失败!";
		}
		echo json_encode($data);
	}
	/*
	 *根据用户id查找用户信息 
	 */
	public function change_uid_to_user($uid){
		$whereArr = array("uid"=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user;
		}
	}
}